package org.wang.generalcode.modules.gen.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.wang.generalcode.modules.gen.pojo.entity.GenTable;
import org.wang.generalcode.modules.gen.pojo.entity.GenTableField;
import org.wang.generalcode.modules.gen.service.TableFieldService;
import org.wang.generalcode.modules.gen.service.TableService;
import org.wang.generalcode.support.mybaties.Condition;
import org.wang.generalcode.support.mybaties.Query;
import org.wang.generalcode.support.result.Result;
import org.wang.generalcode.support.utils.Func;

import java.util.List;

@RestController
@AllArgsConstructor
@RequestMapping("/table")
@Schema(description = "代码生成", name = "代码生成接口")
public class TableController {
    private final TableService tableService;
    private final TableFieldService tableFieldService;

    /**
     * 分页
     *
     * @param query 查询参数
     */
    @ApiOperationSupport(order = 1)
    @Schema(description = "分页查询", example = "传入query")
    @GetMapping("page")
    public Result<IPage<GenTable>> page(Query query) {
        IPage<GenTable> page = tableService.page(Condition.getPage(query));
        return Result.success(page);
    }

    /**
     * 获取表信息
     *
     * @param id 表ID
     */
    @ApiOperationSupport(order = 3)
    @Schema(description = "查询详情", example = "")
    @GetMapping("/detail")
    public Result<GenTable> get(@Parameter(name = "主键", required = true) @RequestParam Long id) {
        GenTable table = tableService.getById(id);

        // 获取表的字段
        List<GenTableField> fieldList = tableFieldService.getByTableId(table.getId());
        table.setFieldList(fieldList);

        return Result.success(table);
    }

    /**
     * 修改
     *
     * @param table 表信息
     */
    @ApiOperationSupport(order = 6)
    @Schema(description = "修改", example = "")
    @PostMapping("/update")
    public Result<String> update(@RequestBody GenTable table) {
        return Result.judge(tableService.updateById(table));
    }

    /**
     * 删除
     *
     * @param ids 表id数组
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 7)
    @Schema(description = "删除", example = "传入ids")
    public Result<String> remove(@Parameter(name = "主键集合", required = true) @RequestParam String ids) {
        return Result.judge(tableService.deleteBatchIds(Func.toLongList(ids)));
    }

    /**
     * 同步表结构
     *
     * @param id 表ID
     */
    @ApiOperationSupport(order = 8)
    @Schema(description = "同步表结构", example = "传入id")
    @PostMapping("/sync")
    public Result<String> sync(@Parameter(name = "主键", required = true) @RequestParam Long id) {
        return Result.judge(tableService.sync(id));
    }

    /**
     * 导入数据源中的表
     *
     * @param datasourceId  数据源ID
     * @param tableNameList 表名列表
     */
    @ApiOperationSupport(order = 9)
    @Schema(description = "导入数据源中的表", example = "传入datasourceId")
    @PostMapping("import")
    public Result<String> tableImport(@Parameter(name = "数据源id", required = true) @RequestParam Long datasourceId, @RequestBody List<String> tableNameList) {
        for (String tableName : tableNameList) {
            tableService.tableImport(datasourceId, tableName);
        }

        return Result.success();
    }

    /**
     * 修改表字段数据
     *
     * @param tableId        表ID
     * @param tableFieldList 字段列表
     */
    @ApiOperationSupport(order = 10)
    @Schema(description = "修改表字段数据", example = "传入tableId")
    @PutMapping("/field")
    public Result<String> updateTableField(@Parameter(name = "表格id", required = true) @RequestParam Long tableId, @RequestBody List<GenTableField> tableFieldList) {
        return Result.judge(tableFieldService.updateTableField(tableId, tableFieldList));
    }
}
